package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicLinePrice;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLinePriceParam;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicLinePriceService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 承运报价表控制器
 *
 * @author zhucx
 * @Date 2021-07-29 11:22:13
 */
@Controller
@RequestMapping("/basicLinePrice")
public class BasicLinePriceController extends BaseController {

    private String PREFIX = "/basic/basicLinePrice";

    @Autowired
    private BasicLinePriceService basicLinePriceService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicLinePrice.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deliverId = user.getDeliverId();
    	//查询可选择承运商
    	BasicCarrierParam basicCarrierParam = new BasicCarrierParam();
    	basicCarrierParam.setDeliverId(deliverId);
    	model.addAttribute("carrierlist", basicCarrierService.findListBySpec(basicCarrierParam,null));
        return PREFIX + "/basicLinePrice_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deliverId = user.getDeliverId();
    	//查询可选择承运商
    	BasicCarrierParam basicCarrierParam = new BasicCarrierParam();
    	basicCarrierParam.setDeliverId(deliverId);
    	model.addAttribute("carrierlist", basicCarrierService.findListBySpec(basicCarrierParam,null));
        return PREFIX + "/basicLinePrice_edit.html";
    }

    /**
     * 选择承运商页面
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/toCarrierName")
    public String toCarrierName(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/chooseCarrierName.html";
    }

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicLineService basicLineService;

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicLinePriceParam basicLinePriceParam) {
    	Object carrierId = basicLinePriceParam.getCarrierId();
    	if (carrierId != null) {
    		BasicCarrier basicCarrier = basicCarrierService.getById(Integer.parseInt(carrierId.toString()));
    		basicLinePriceParam.setCarrierName(basicCarrier.getCarrierName());
		}
    	
    	BasicLine basicLine = basicLineService.getById(basicLinePriceParam.getLineId());
    	basicLinePriceParam.setLineName(basicLine.getLineName());
    	basicLinePriceParam.setCreateTimes(DateUtil.getDateS());
    	basicLinePriceParam.setCreateName(userService.getcurrentUser().getName());
        this.basicLinePriceService.add(basicLinePriceParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicLinePriceParam basicLinePriceParam) {
    	Object carrierId = basicLinePriceParam.getCarrierId();
    	if (carrierId != null) {
    		BasicCarrier basicCarrier = basicCarrierService.getById(Integer.parseInt(carrierId.toString()));
    		basicLinePriceParam.setCarrierName(basicCarrier.getCarrierName());
		}
        this.basicLinePriceService.update(basicLinePriceParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicLinePriceParam basicLinePriceParam) {
        this.basicLinePriceService.delete(basicLinePriceParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicLinePriceParam basicLinePriceParam) {
        BasicLinePrice detail = this.basicLinePriceService.getById(basicLinePriceParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2021-07-29
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicLinePriceParam basicLinePriceParam,String condition ) {
        return this.basicLinePriceService.findPageBySpec(basicLinePriceParam,condition);
    }

}


